package com.gt.aep.web.systemManage;

import com.alibaba.fastjson.JSONObject;
import com.gt.aep.base.entity.systemManage.SysIdentityRole;
import com.gt.aep.base.entity.util.DateTime;
import com.gt.aep.base.entity.util.Result;
import com.gt.aep.base.service.systemManage.SysIdentityRoleService;
import com.gt.aep.base.service.systemManage.SysPermissionIdentityRoleService;
import com.gt.aep.base.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("sysIdentityRole")
public class SysIdentityRoleController {

    @Autowired
    SysIdentityRoleService sysIdentityRoleService;
    @Autowired
    SysPermissionIdentityRoleService sysPermissionIdentityRoleService;

    /**
     * 新增/修改系统角色
     * @param data
     * @param encryptAesKey
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public Map<String, Object> save(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        SysIdentityRole sysIdentityRole = (SysIdentityRole) dataObject.get("sysRole");
        String loginUid = (String) dataObject.get("loginUid");
        //判断是修改还是新增角色
        if(StringUtils.isEmpty(sysIdentityRole.getId())){
            if (sysIdentityRoleService.getRoleCountByName(sysIdentityRole.getName())>0){
                return Result.error("该角色已经存在");
            }else {
                sysIdentityRole.setId(UUID.randomUUID().toString());
                sysIdentityRole.setCreateTime(DateTime.time());
                sysIdentityRoleService.insert(sysIdentityRole);
                return Result.success("新增角色成功");
            }
        }else{
            sysIdentityRole.setUpdater(loginUid);
            sysIdentityRole.setUpdateTime(DateTime.time());
            sysIdentityRoleService.update(sysIdentityRole);
            return Result.success("编辑角色成功");
        }
    }

    /**
     * 批量删除角色
     */
    @ResponseBody
    @RequestMapping("deleteRole")
    public Map<String, Object> deleteUser(String data,String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        String[] ids = (String[]) dataObject.get("ids");

        //数据库操作
        int j = sysIdentityRoleService.delete(ids);
        //删除关联表相关关系
        for (int i = 0; i < ids.length; i++){
            try{
                //删除身份角色-用户相关信息
                sysIdentityRoleService.deleteByIdentityRoleId(ids[i]);
                //删除身份角色-权限关系表信息
                sysPermissionIdentityRoleService.deleteByIdentityRid(ids[i]);
            }catch (Exception e){
                return Result.error("删除角色-用户关系失败");
            }
        }
        if (j>0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    /**
     * 为系统角色管理首页获取角色列表
     */
    @ResponseBody
    @RequestMapping("getRoleList")
    public Map<String, Object> getRoleList(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
//        int curr,int limit,String userName,String userId,

        System.out.println("jsPublicKey:"+jsPublicKey);
        SysIdentityRole sysIdentityRole = new SysIdentityRole();
        //数据库查询
        List<SysIdentityRole> l = sysIdentityRoleService.getList(sysIdentityRole);
        //加密要传出的数据
        return EncryptUtil.encryptData(l,jsPublicKey);
    }

}
